const { router, fs, urlencodedParser } = require('../CustomMethod');
const database = require('../CustomMethod/mysqlMethod');

// 发送聊天信息
router.post('/chat', urlencodedParser, async (req, res) => {
    let { message, time, paramValue } = req.body;
    const addSqlParams = [paramValue, message, time];
    database('INSERT INTO messagetable (user_id,chat,time) VALUES(?,?,?)', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "用户名已存在" });
            return;
        }
        database('SELECT id,avatar,`name` FROM userinformation WHERE id=' + paramValue, function (err, result) {
            if (err) {
                res.send({ code: 0, module: "获取失败" });
                return;
            }
            res.send({ code: 200, module: result });
        })
    });
});


// 获取聊天记录
router.post('/chatmysql', urlencodedParser, async (req, res) => {
    database("SELECT id,avatar,`name`,chat,`time` FROM userinformation LEFT JOIN messagetable ON id=user_id WHERE `time` IS NOT NULL AND `time` <> ''", function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

// 获取用户列表
router.post('/userlist', urlencodedParser, async (req, res) => {
    database('SELECT id,account,avatar,`name`,resume FROM userinformation', function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//个人介绍
router.post('/userdetail', urlencodedParser, async (req, res) => {
    let id = req.body.id;
    database('SELECT id,account,avatar,`name`,resume FROM userinformation WHERE id=' + id, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})


//访问好友表
router.post('/FL', urlencodedParser, async (req, res) => {
    let paramValue = req.body.paramValue;
    database('SELECT friend_id FROM friendtable WHERE user_id=' + paramValue, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//加好友
router.post('/friendlist', urlencodedParser, async (req, res) => {
    let { paramValue, friend_id, time } = req.body;
    const addSqlParams = [paramValue, friend_id, time];
    database('INSERT INTO friendtable (user_id,friend_id,time) VALUES(?,?,?)', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "添加失败" });
            return;
        }
        database('INSERT INTO friendtable (user_id,friend_id,time) VALUES(?,?,?)', [friend_id, paramValue, time], function (err, result) {
            if (err) {
                res.send({ code: 0, module: "添加失败" });
                return;
            }
            res.send({ code: 200, module: "添加成功" });
        })
    });
})

//删除好友
router.post('/deleteFriend', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [paramValue, id];
    //删除双方好友
    database('DELETE FROM friendtable WHERE user_id=? AND friend_id=?', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "删除失败" });
            return;
        }
        database('DELETE FROM friendtable WHERE user_id=? AND friend_id=?', [id, paramValue], function (err, result) {
            if (err) {
                res.send({ code: 0, module: "删除失败" });
                return;
            }
            res.send({ code: 200, module: "删除成功" });
        })
    });
})

//获取好友列表
router.post('/friend', urlencodedParser, async (req, res) => {
    let paramValue = req.body.paramValue;
    database('SELECT id,account,avatar,`name`,resume FROM userinformation WHERE id IN (SELECT friend_id FROM friendtable WHERE user_id=' + paramValue + ')', function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//将聊天记录存入数据库
router.post('/privatechat', urlencodedParser, async (req, res) => {
    let { paramValue, id, message, time } = req.body;
    const addSqlParams = [paramValue, id, time, message];
    database('INSERT INTO privatechattable (user1,user2,time,message) VALUES(?,?,?,?)', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "添加失败" });
            return;
        }
        database('SELECT ui.id, ui.avatar, ui.`name` FROM userinformation ui WHERE ui.id IN (?)', [paramValue], function (err, result) {
            if (err) {
                res.send({ code: 0, module: "获取失败" });
                return;
            }
            res.send({ code: 200, module: result });
        })
    });
})

//页面刷新时获取头像和昵称(请勿修改)
router.post('/getname', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    //获得这两个id的信息
    database('SELECT ui.id, ui.avatar, ui.`name`, mt.message, mt.`time`  FROM userinformation ui LEFT JOIN privatechattable mt ON ui.id = mt.user1 WHERE (mt.user1 = ? AND mt.user2 = ?) OR (mt.user1 = ? AND mt.user2 = ?) AND mt.`time` IS NOT NULL', [paramValue, id, id, paramValue], function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})





//!后续待开发
//获取未读消息
router.post('/unread', urlencodedParser, async (req, res) => {
    let { paramValue } = req.body;
    database('SELECT COUNT(*) as count FROM privatechattable WHERE user1=? AND is_read=0', [paramValue], function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//标记已读
router.post('/read', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [1, id];
    database('UPDATE privatechattable SET is_read=? WHERE user1=? AND user2=?', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "标记失败" });
            return;
        }
        res.send({ code: 200, module: "标记成功" });
    });
})

//获取未读消息列表
router.post('/unreadlist', urlencodedParser, async (req, res) => {
    let { paramValue } = req.body;
    database('SELECT * FROM privatechattable WHERE user1=? AND is_read=0', [paramValue], function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//获取好友申请列表
router.post('/applylist', urlencodedParser, async (req, res) => {
    let { paramValue } = req.body;
    database('SELECT * FROM friendapplytable WHERE user_id=' + paramValue, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

//同意好友申请
router.post('/agree', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [1, id];
    database('UPDATE friendapplytable SET status=? WHERE user_id=? AND friend_id=?', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "同意失败" });
            return;
        }
        database('INSERT INTO friendtable (user_id,friend_id,time) VALUES(?,?,?)', [paramValue, id, new Date().getTime()], function (err, result) {
            if (err) {
                res.send({ code: 0, module: "添加失败" });
                return;
            }
            database('INSERT INTO friendtable (user_id,friend_id,time) VALUES(?,?,?)', [id, paramValue, new Date().getTime()], function (err, result) {
                if (err) {
                    res.send({ code: 0, module: "添加失败" });
                    return;
                }
                res.send({ code: 200, module: "同意成功" });
            })
        })
    });
})

//拒绝好友申请
router.post('/refuse', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [2, id];
    database('UPDATE friendapplytable SET status=? WHERE user_id=? AND friend_id=?', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "拒绝失败" });
            return;
        }
        res.send({ code: 200, module: "拒绝成功" });
    });
})

//删除好友申请
router.post('/deleteapply', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [paramValue, id];
    database('DELETE FROM friendapplytable WHERE user_id=? AND friend_id=?', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "删除失败" });
            return;
        }
        res.send({ code: 200, module: "删除成功" });
    });
})

//发送好友申请
router.post('/apply', urlencodedParser, async (req, res) => {
    let { paramValue, id } = req.body;
    const addSqlParams = [paramValue, id, new Date().getTime()];
    database('INSERT INTO friendapplytable (user_id,friend_id,time) VALUES(?,?,?)', addSqlParams, function (err, result) {
        if (err) {
            res.send({ code: 0, module: "发送失败" });
            return;
        }
        res.send({ code: 200, module: "发送成功" });
    });
})
module.exports = router;